package com.example.inventorywarningdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.inventorywarningdemo.converter.MaterialConverter;
import com.example.inventorywarningdemo.dto.MaterialDTO;
import com.example.inventorywarningdemo.entity.Material;
import com.example.inventorywarningdemo.mapper.MaterialMapper;
import com.example.inventorywarningdemo.service.MaterialQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class MaterialQueryServiceImpl implements MaterialQueryService {
    private final MaterialMapper materialMapper;
    private final MaterialConverter materialConverter;

    /**
     * 查询实际库存少于阈值的原材料
     * @return
     */
    @Override
    public List<MaterialDTO> getMaterialsBelowThreshold() {
        List<Material> materials = materialMapper.selectList(
            new LambdaQueryWrapper<Material>()
                    .apply("stock_quantity < min_stock_threshold") // 改为使用SQL表达式
        );
        return materials.stream()
                .map(materialConverter::entityToDto)
                .collect(Collectors.toList());
    }
}